home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / kgdb.mips / mipscoff.save < prev    next >
Text File  |  1991-07-30  |  57KB  |  2,069 lines

  1. /* Read coff symbol tables and convert to internal format, for GDB.
  2.    Design and support routines derived from dbxread.c, and UMAX COFF
  3.    specific routines written 9/1/87 by David D. Johnson, Brown University.
  4.    Revised 11/27/87 ddj@cs.brown.edu
  5.    Copyright (C) 1987, 1988, 1989 Free Software Foundation, Inc.
  6.  
  7. This file is part of GDB.
  8.  
  9. GDB is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 1, or (at your option)
  12. any later version.
  13.  
  14. GDB is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with GDB; see the file COPYING.  If not, write to
  21. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23.  
  24. #include <a.out.h>
  25. #include <stdio.h>
  26. #include "symtab.h"
  27. #include <sys/file.h>
  28.  
  29. PDR *proc_desc_table = NULL;
  30. long proc_desc_length = 0;
  31.  
  32. static void add_symbol_to_list ();
  33. static int read_coff_symtab ();
  34. static void patch_opaque_types ();
  35. static struct type *decode_function_type ();
  36. static struct type *decode_type ();
  37. static struct type *decode_base_type ();
  38. static struct type *read_enum_type ();
  39. static struct type *read_struct_type ();
  40. static struct type *read_type ();
  41. static void finish_block ();
  42. static struct blockvector *make_blockvector ();
  43. static struct symbol *process_coff_symbol ();
  44. static char *getfilename ();
  45. static char *getsymname ();
  46.  
  47. extern int fclose ();
  48. extern void close ();
  49. extern void free_all_symtabs ();
  50. extern void free_all_psymtabs ();
  51.  
  52. int debug = 0;
  53.  
  54.  
  55. /* Name of source file whose symbol data we are now processing.
  56.    This comes from a symbol named ".file".  */
  57.  
  58. static char *last_source_file;
  59.  
  60. /* Core address of the end of the first object file.  */
  61. static CORE_ADDR first_object_file_end;
  62.  
  63. /* End of the text segment of the executable file,
  64.    as found in the symbol _etext.  */
  65.  
  66. static CORE_ADDR end_of_text_addr;
  67.  
  68. /* The end address of the last seen procedure. */
  69.  
  70. static CORE_ADDR last_end_addr;
  71.  
  72. /* The file, a.out  and text section headers of the symbol file */
  73.  
  74. static FILHDR file_hdr;
  75. static SCNHDR text_hdr;
  76. static AOUTHDR aout_hdr;
  77.  
  78. /* The index in the symbol table of the last coff symbol that was processed.  */
  79.  
  80. static int symnum;
  81.  
  82. /* Vector of types defined so far, indexed by their coff symnum.  */
  83.  
  84. static struct typevector *type_vector;
  85.  
  86. /* Number of elements allocated for type_vector currently.  */
  87.  
  88. static int type_vector_length;
  89.  
  90. /* Chain of typedefs of pointers to empty struct/union types.
  91.    They are chained thru the SYMBOL_VALUE.  */
  92.  
  93. #define HASHSIZE 127
  94. static struct symbol *opaque_type_chain[HASHSIZE];
  95.  
  96. /* Record the symbols defined for each context in a list.
  97.    We don't create a struct block for the context until we
  98.    know how long to make it.  */
  99.  
  100. struct pending
  101. {
  102.   struct pending *next;
  103.   struct symbol *symbol;
  104. };
  105.  
  106. /* Here are the three lists that symbols are put on.  */
  107.  
  108. struct pending *file_symbols;    /* static at top level, and types */
  109.  
  110. struct pending *global_symbols;    /* global functions and variables */
  111.  
  112. struct pending **global_symbols_all, **file_symbols_all;
  113.  
  114. struct pending *local_symbols;    /* everything local to lexical context */
  115.  
  116. /* List of unclosed lexical contexts
  117.    (that will become blocks, eventually).  */
  118.  
  119. struct context_stack
  120. {
  121.   struct context_stack *next;
  122.   struct pending *locals;
  123.   struct pending_block *old_blocks;
  124.   struct symbol *name;
  125.   CORE_ADDR start_addr;
  126. };
  127.  
  128. struct context_stack *context_stack;
  129.  
  130. /* Nonzero if within a function (so symbols should be local,
  131.    if nothing says specifically).  */
  132.  
  133. int within_function;
  134.  
  135. /* List of blocks already made (lexical contexts already closed).
  136.    This is used at the end to make the blockvector.  */
  137.  
  138. struct pending_block
  139. {
  140.   struct pending_block *next;
  141.   struct block *block;
  142. };
  143.  
  144. struct pending_block *pending_blocks;
  145.  
  146. extern CORE_ADDR startup_file_start;    /* From blockframe.c */
  147. extern CORE_ADDR startup_file_end;    /* From blockframe.c */
  148.  
  149. /* File name symbols were loaded from.  */
  150.  
  151. static char *symfile;
  152.  
  153. /* Look up a coff type-number index.  Return the address of the slot
  154.    where the type for that index is stored.
  155.    The type-number is in INDEX. 
  156.  
  157.    This can be used for finding the type associated with that index
  158.    or for associating a new type with the index.  */
  159.  
  160. static struct type **
  161. coff_lookup_type (index)
  162.      register int index;
  163. {
  164.   if (index >= type_vector_length)
  165.     {
  166.       int old_vector_length = type_vector_length;
  167.  
  168.       type_vector_length *= 2;
  169.       if (type_vector_length < index) {
  170.     type_vector_length = index * 2;
  171.       }
  172.       type_vector = (struct typevector *)
  173.     xrealloc (type_vector, sizeof (struct typevector)
  174.                 + type_vector_length * sizeof (struct type *));
  175.       bzero (&type_vector->type[ old_vector_length ],
  176.          (type_vector_length - old_vector_length) * sizeof(struct type *));
  177.     }
  178.   return &type_vector->type[index];
  179. }
  180.  
  181. /* Make sure there is a type allocated for type number index
  182.    and return the type object.
  183.    This can create an empty (zeroed) type object.  */
  184.  
  185. static struct type *
  186. coff_alloc_type (index)
  187.      int index;
  188. {
  189.   register struct type **type_addr = coff_lookup_type (index);
  190.   register struct type *type = *type_addr;
  191.  
  192.   /* If we are referring to a type not known at all yet,
  193.      allocate an empty type for it.
  194.      We will fill it in later if we find out how.  */
  195.   if (type == 0)
  196.     {
  197.       type = (struct type *) obstack_alloc (symbol_obstack,
  198.                         sizeof (struct type));
  199.       bzero (type, sizeof (struct type));
  200.       *type_addr = type;
  201.     }
  202.   return type;
  203. }
  204.  
  205. /* maintain the lists of symbols and blocks */
  206.  
  207. /* Add a symbol to one of the lists of symbols.  */
  208. static void
  209. add_symbol_to_list (symbol, listhead)
  210.      struct symbol *symbol;
  211.      struct pending **listhead;
  212. {
  213.   register struct pending *link
  214.     = (struct pending *) xmalloc (sizeof (struct pending));
  215.  
  216.   link->next = *listhead;
  217.   link->symbol = symbol;
  218.   *listhead = link;
  219. }
  220.  
  221. /* Take one of the lists of symbols and make a block from it.
  222.    Put the block on the list of pending blocks.  */
  223.  
  224. static void
  225. finish_block (symbol, listhead, old_blocks, start, end)
  226.      struct symbol *symbol;
  227.      struct pending **listhead;
  228.      struct pending_block *old_blocks;
  229.      CORE_ADDR start, end;
  230. {
  231.   register struct pending *next, *next1;
  232.   register struct block *block;
  233.   register struct pending_block *pblock;
  234.   struct pending_block *opblock;
  235.   register int i;
  236.  
  237.   /* Count the length of the list of symbols.  */
  238.  
  239.   for (next = *listhead, i = 0; next; next = next->next, i++);
  240.  
  241.   block = (struct block *)
  242.         obstack_alloc (symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
  243.  
  244.   /* Copy the symbols into the block.  */
  245.  
  246.   BLOCK_NSYMS (block) = i;
  247.   for (next = *listhead; next; next = next->next)
  248.     BLOCK_SYM (block, --i) = next->symbol;
  249.  
  250.   BLOCK_START (block) = start;
  251.   BLOCK_END (block) = end;
  252.   BLOCK_SUPERBLOCK (block) = 0;    /* Filled in when containing block is made */
  253.  
  254.   /* Put the block in as the value of the symbol that names it.  */
  255.  
  256.   if (symbol)
  257.     {
  258.       SYMBOL_BLOCK_VALUE (symbol) = block;
  259.       BLOCK_FUNCTION (block) = symbol;
  260.     }
  261.   else
  262.     BLOCK_FUNCTION (block) = 0;
  263.  
  264.   /* Now free the links of the list, and empty the list.  */
  265.  
  266.   for (next = *listhead; next; next = next1)
  267.     {
  268.       next1 = next->next;
  269.       free (next);
  270.     }
  271.   *listhead = 0;
  272.  
  273.   /* Install this block as the superblock
  274.      of all blocks made since the start of this scope
  275.      that don't have superblocks yet.  */
  276.  
  277.   opblock = 0;
  278.   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
  279.     {
  280.       if (BLOCK_SUPERBLOCK (pblock->block) == 0)
  281.     BLOCK_SUPERBLOCK (pblock->block) = block;
  282.       opblock = pblock;
  283.     }
  284.  
  285.   /* Record this block on the list of all blocks in the file.
  286.      Put it after opblock, or at the beginning if opblock is 0.
  287.      This puts the block in the list after all its subblocks.  */
  288.  
  289.   pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
  290.   pblock->block = block;
  291.   if (opblock)
  292.     {
  293.       pblock->next = opblock->next;
  294.       opblock->next = pblock;
  295.     }
  296.   else
  297.     {
  298.       pblock->next = pending_blocks;
  299.       pending_blocks = pblock;
  300.     }
  301. }
  302.  
  303. static struct blockvector *
  304. make_blockvector ()
  305. {
  306.   register struct pending_block *next, *next1;
  307.   register struct blockvector *blockvector;
  308.   register int i;
  309.  
  310.   /* Count the length of the list of blocks.  */
  311.  
  312.   for (next = pending_blocks, i = 0; next; next = next->next, i++);
  313.  
  314.   blockvector = (struct blockvector *)
  315.           obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
  316.  
  317.   /* Copy the blocks into the blockvector.
  318.      This is done in reverse order, which happens to put
  319.      the blocks into the proper order (ascending starting address).
  320.      finish_block has hair to insert each block into the list
  321.      after its subblocks in order to make sure this is true.  */
  322.  
  323.   BLOCKVECTOR_NBLOCKS (blockvector) = i;
  324.   for (next = pending_blocks; next; next = next->next)
  325.     BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
  326.  
  327.   /* Now free the links of the list, and empty the list.  */
  328.  
  329.   for (next = pending_blocks; next; next = next1)
  330.     {
  331.       next1 = next->next;
  332.       free (next);
  333.     }
  334.   pending_blocks = 0;
  335.  
  336.   return blockvector;
  337. }
  338.  
  339. /* Manage the vector of line numbers.  */
  340.  
  341. static
  342. record_line (line, pc)
  343.      int line;
  344.      CORE_ADDR pc;
  345. {
  346. }
  347.  
  348. /* Start a new symtab for a new source file.
  349.    This is called when a COFF ".file" symbol is seen;
  350.    it indicates the start of data for one original source file.  */
  351.  
  352. static void
  353. start_symtab ()
  354. {
  355.   context_stack = 0;
  356.   within_function = 0;
  357.   last_source_file = 0;
  358.  
  359.   /* Initialize the source file information for this file.  */
  360.  
  361. }
  362.  
  363. /* Finish the symbol definitions for one main source file,
  364.    close off all the lexical contexts for that file
  365.    (creating struct block's for them), then make the
  366.    struct symtab for that file and put it in the list of all such.
  367.  
  368.    END_ADDR is the address of the end of the file's text.  */
  369.  
  370. static void
  371. end_symtab (start_addr, end_addr, linetable)
  372.      CORE_ADDR start_addr, end_addr;
  373.      struct linetable *linetable;
  374. {
  375.   register struct symtab *symtab;
  376.   register struct context_stack *cstk;
  377.   register struct blockvector *blockvector;
  378.   struct partial_symtab *psymtab;
  379.  
  380.   if (aout_hdr.entry < end_addr
  381.       && aout_hdr.entry >= start_addr)
  382.     {
  383.       startup_file_start = start_addr;
  384.       startup_file_end = end_addr;
  385.     }
  386.  
  387.   /* Finish the lexical context of the last function in the file.  */
  388.  
  389.   if (context_stack)
  390.     {
  391.       cstk = context_stack;
  392.       context_stack = 0;
  393.       /* Make a block for the local symbols within.  */
  394.       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
  395.             cstk->start_addr, end_addr);
  396.       free (cstk);
  397.     }
  398.  
  399.   /* Create the two top-level blocks for this file.  */
  400.   finish_block (0, &file_symbols, 0, start_addr, end_addr);
  401.   finish_block (0, &global_symbols, 0, start_addr, end_addr);
  402.  
  403.   /* Create the blockvector that points to all the file's blocks.  */
  404.   blockvector = make_blockvector ();
  405.  
  406.   /* Now create the symtab object for this source file.  */
  407.   symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
  408.   symtab->free_ptr = 0;
  409.  
  410.   /* Fill in its components.  */
  411.   symtab->blockvector = blockvector;
  412.   symtab->free_code = free_linetable;
  413.   symtab->filename = last_source_file;
  414.   symtab->linetable = linetable;
  415.   symtab->nlines = 0;
  416.   symtab->line_charpos = 0;
  417.  
  418.   /* Link the new symtab into the list of such.  */
  419.   symtab->next = symtab_list;
  420.   symtab_list = symtab;
  421.  
  422.   /* Reinitialize for beginning of new file. */
  423.   last_source_file = 0;
  424.  
  425.   /* Create a fake partial_symtab, so that find_pc_partial_function
  426.    * will do the right thing. */
  427.   psymtab = (struct partial_symtab *)
  428.       obstack_alloc (psymbol_obstack,
  429.              sizeof (struct partial_symtab));
  430.   bzero (psymtab, sizeof (struct partial_symtab));
  431.   psymtab->next = partial_symtab_list;
  432.   partial_symtab_list = psymtab;
  433.   psymtab->textlow = start_addr;
  434.   psymtab->texthigh = end_addr;
  435.   psymtab->filename = symtab->filename ? symtab->filename : "";
  436.   psymtab->readin = 1;
  437.  
  438. }
  439.  
  440. /* Accumulate the misc functions in bunches of 127.
  441.    At the end, copy them all into one newly allocated structure.  */
  442.  
  443. #define MISC_BUNCH_SIZE 127
  444.  
  445. struct misc_bunch
  446. {
  447.   struct misc_bunch *next;
  448.   struct misc_function contents[MISC_BUNCH_SIZE];
  449. };
  450.  
  451. /* Bunch currently being filled up.
  452.    The next field points to chain of filled bunches.  */
  453.  
  454. static struct misc_bunch *misc_bunch;
  455.  
  456. /* Number of slots filled in current bunch.  */
  457.  
  458. static int misc_bunch_index;
  459.  
  460. /* Total number of misc functions recorded so far.  */
  461.  
  462. static int misc_count;
  463.  
  464. static void
  465. init_misc_functions ()
  466. {
  467.   misc_count = 0;
  468.   misc_bunch = 0;
  469.   misc_bunch_index = MISC_BUNCH_SIZE;
  470. }
  471.  
  472. static void
  473. record_misc_function (name, address)
  474.      char *name;
  475.      CORE_ADDR address;
  476. {
  477.   register struct misc_bunch *new;
  478.  
  479.   if (misc_bunch_index == MISC_BUNCH_SIZE)
  480.     {
  481.       new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
  482.       misc_bunch_index = 0;
  483.       new->next = misc_bunch;
  484.       misc_bunch = new;
  485.     }
  486.   misc_bunch->contents[misc_bunch_index].name = savestring (name, strlen (name));
  487.   misc_bunch->contents[misc_bunch_index].address = address;
  488.   misc_bunch->contents[misc_bunch_index].type = (char)mf_unknown;
  489.   misc_bunch_index++;
  490.   misc_count++;
  491. }
  492.  
  493. /* if we see a function symbol, we do record_misc_function.
  494.  * however, if it turns out the next symbol is '.bf', then
  495.  * we call here to undo the misc definition
  496.  */
  497. static void
  498. unrecord_misc_function ()
  499. {
  500.   if (misc_bunch_index == 0)
  501.     error ("Internal error processing symbol table, at symbol %d.",
  502.        symnum);
  503.   misc_bunch_index--;
  504.   misc_count--;
  505. }
  506.  
  507.  
  508. static int
  509. compare_misc_functions (fn1, fn2)
  510.      struct misc_function *fn1, *fn2;
  511. {
  512.   /* Return a signed result based on unsigned comparisons
  513.      so that we sort into unsigned numeric order.  */
  514.   if (fn1->address < fn2->address)
  515.     return -1;
  516.   if (fn1->address > fn2->address)
  517.     return 1;
  518.   return 0;
  519. }
  520.  
  521. static void
  522. discard_misc_bunches ()
  523. {
  524.   register struct misc_bunch *next;
  525.  
  526.   while (misc_bunch)
  527.     {
  528.       next = misc_bunch->next;
  529.       free (misc_bunch);
  530.       misc_bunch = next;
  531.     }
  532. }
  533.  
  534. static void
  535. condense_misc_bunches ()
  536. {
  537.   register int i, j;
  538.   register struct misc_bunch *bunch;
  539. #ifdef NAMES_HAVE_UNDERSCORE
  540.   int offset = 1;
  541. #else
  542.   int offset = 0;
  543. #endif
  544.  
  545.   misc_function_vector
  546.     = (struct misc_function *)
  547.       xmalloc (misc_count * sizeof (struct misc_function));
  548.  
  549.   j = 0;
  550.   bunch = misc_bunch;
  551.   while (bunch)
  552.     {
  553.       for (i = 0; i < misc_bunch_index; i++)
  554.     {
  555.       register char *tmp;
  556.  
  557.       misc_function_vector[j] = bunch->contents[i];
  558.       tmp = misc_function_vector[j].name;
  559.       misc_function_vector[j].name = (tmp[0] == '_' ? tmp + offset : tmp);
  560.       j++;
  561.     }
  562.       bunch = bunch->next;
  563.       misc_bunch_index = MISC_BUNCH_SIZE;
  564.     }
  565.  
  566.   misc_function_count = j;
  567.  
  568.   /* Sort the misc functions by address.  */
  569.  
  570.   qsort (misc_function_vector, j, sizeof (struct misc_function),
  571.      compare_misc_functions);
  572. }
  573.  
  574. /* Call sort_syms to sort alphabetically
  575.    the symbols of each block of each symtab.  */
  576.  
  577. static int
  578. compare_symbols (s1, s2)
  579.      struct symbol **s1, **s2;
  580. {
  581.   /* Names that are less should come first.  */
  582.   register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
  583.   if (namediff != 0) return namediff;
  584.   /* For symbols of the same name, registers should come first.  */
  585.   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
  586.       - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
  587. }
  588.  
  589. static void
  590. sort_syms ()
  591. {
  592.   register struct symtab *s;
  593.   register int i, nbl;
  594.   register struct blockvector *bv;
  595.   register struct block *b;
  596.  
  597.   for (s = symtab_list; s; s = s->next)
  598.     {
  599.       bv = BLOCKVECTOR (s);
  600.       nbl = BLOCKVECTOR_NBLOCKS (bv);
  601.       for (i = 0; i < nbl; i++)
  602.     {
  603.       b = BLOCKVECTOR_BLOCK (bv, i);
  604.       if (BLOCK_SHOULD_SORT (b))
  605.           qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
  606.              sizeof (struct symbol *), compare_symbols);
  607.     }
  608.     }
  609. }
  610.  
  611. /* Call sort_proc_descs to sort the procedure descriptors by address.  */
  612.  
  613. static int
  614. compare_proc_descs (p1, p2)
  615. PDR *p1, *p2;
  616. {
  617.   unsigned int a1, a2;
  618.  
  619.   a1 = PROC_LOW_ADDR (p1);
  620.   a2 = PROC_LOW_ADDR (p2);
  621.   if (a1 < a2)
  622.     return -1;
  623.   if (a1 > a2)
  624.     return 1;
  625.   return 0;
  626. }
  627.  
  628. static void
  629. sort_proc_descs ()
  630. {
  631.   qsort (proc_desc_table, proc_desc_length,
  632.      sizeof (PDR), compare_proc_descs);
  633. }
  634.  
  635.  
  636. /* This is the symbol-file command.  Read the file, analyze its symbols,
  637.    and add a struct symtab to symtab_list.  */
  638.  
  639. /* !!! !!! */
  640. int dump_stuff=0;
  641.  
  642. static void free_proc_descs ()
  643. {
  644.     if (proc_desc_table != NULL) {
  645.     free (proc_desc_table);
  646.     proc_desc_table = NULL;
  647.     proc_desc_length = 0;
  648.     }
  649. }
  650.  
  651. void
  652. symbol_file_command (name)
  653.      char *name;
  654. {
  655.   int desc;
  656.   int num_symbols;
  657.   int num_sections;
  658.   int symtab_offset;
  659.   register int val;
  660.   struct cleanup *old_chain;
  661.  
  662.   dont_repeat ();
  663.  
  664.   if (name == 0)
  665.     {
  666.       if (symtab_list && !query ("Discard symbol table? ", 0))
  667.     error ("Not confirmed.");
  668.       if (symfile)
  669.     free (symfile);
  670.       symfile = 0;
  671.       free_all_symtabs ();
  672.       free_proc_descs ();
  673.       return;
  674.     }
  675.  
  676.   name = tilde_expand (name);
  677.   make_cleanup (free, name);
  678.  
  679.   if (symtab_list && !query ("Load new symbol table from \"%s\"? ", name))
  680.     error ("Not confirmed.");
  681.  
  682.   if (symfile)
  683.     free (symfile);
  684.   symfile = 0;
  685.  
  686.   {
  687.     char *absolute_name;
  688.  
  689.     desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
  690.     if (desc < 0)
  691.       perror_with_name (name);
  692.     else
  693.       name = absolute_name;
  694.   }
  695.  
  696.   old_chain = make_cleanup (close, desc);
  697.   make_cleanup (free_current_contents, &name);
  698.  
  699.   if ((num_symbols = read_file_hdr (desc, &file_hdr)) < 0)
  700.     error ("File \"%s\" not in executable format.", name);
  701.  
  702.   /* If an a.out header is present, read it in.  If not (e.g. a .o file)
  703.      deal with its absence.  */
  704.   if (file_hdr.f_opthdr == 0
  705.       || read_aout_hdr (desc, &aout_hdr, file_hdr.f_opthdr) < 0)
  706.     {
  707.       /* We will not actually be able to run code, since backtraces would
  708.      fly off the bottom of the stack (there is no way to reliably
  709.      detect bottom of stack), but that's fine since the kernel won't
  710.      run something without an a.out header anyway.  Passive examination
  711.      of .o files is one place this might make sense.  */
  712.       /* ~0 will not be in any file.  */
  713.       aout_hdr.entry = ~0;
  714.       /* set the startup file to be an empty range.  */
  715.       startup_file_start = 0;
  716.       startup_file_end = 0;
  717.     }
  718.  
  719.   if (num_symbols == 0)
  720.     {
  721.       free_all_symtabs ();
  722.       free_proc_descs ();
  723.       printf ("%s does not have a symbol-table.\n", name);
  724.       fflush (stdout);
  725.       do_cleanups (old_chain);
  726.       return;
  727.     }
  728.  
  729.   printf ("Reading symbol data from %s...", name);
  730.   fflush (stdout);
  731.  
  732.   /* Throw away the old symbol table.  */
  733.  
  734.   free_all_symtabs ();
  735.   free_proc_descs ();
  736.   free_all_psymtabs ();        /* Make sure that partial_symtab_list */
  737.                 /* is 0 also. */
  738.  
  739.   num_sections = file_hdr.f_nscns;
  740.   symtab_offset = file_hdr.f_symptr;
  741.  
  742.   if (read_section_hdr (desc, _TEXT, &text_hdr, num_sections) < 0)
  743.     error ("\"%s\": can't read text section header", name);
  744.  
  745.   /* Position to read the symbol table.  Do not read it all at once. */
  746.   val = lseek (desc, (long)symtab_offset, 0);
  747.   if (val < 0)
  748.     perror_with_name (name);
  749.  
  750.   init_misc_functions ();
  751.   make_cleanup (discard_misc_bunches, 0);
  752.  
  753.   /* Now that the executable file is positioned at symbol table,
  754.      process it and define symbols accordingly.  */
  755.  
  756.   val = read_coff_symtab (desc, num_symbols, symtab_offset);
  757.   if (val < 0) error("Bad symbol table format");
  758.  
  759.   patch_opaque_types ();
  760.  
  761.   /* Sort symbols alphabetically within each block.  */
  762.  
  763.   sort_syms ();
  764.  
  765.   /* Go over the misc functions and install them in vector.  */
  766.  
  767.   condense_misc_bunches ();
  768.  
  769.   /* Sort the procedure descriptor table.  */
  770.  
  771.   sort_proc_descs ();
  772.  
  773.   /* Don't allow char * to have a typename (else would get caddr_t.)  */
  774.  
  775.   TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
  776.  
  777.   /* Make a default for file to list.  */
  778.  
  779.   symfile = savestring (name, strlen (name));
  780.  
  781.   do_cleanups (old_chain);
  782.  
  783.   printf ("done.\n");
  784.   fflush (stdout);
  785. }
  786.  
  787. /* Return name of file symbols were loaded from, or 0 if none..  */
  788.  
  789. char *
  790. get_sym_file ()
  791. {
  792.   return symfile;
  793. }
  794.  
  795. /* Simplified internal version of coff symbol table information */
  796.  
  797. struct coff_symbol {
  798.   char *c_name;
  799.   int c_symnum;        /* symbol number of this entry */
  800.   int c_nsyms;        /* 1 if syment only, 2 if syment + auxent */
  801.   long c_value;
  802.   int c_sclass;
  803.   int c_secnum;
  804.   unsigned int c_type;
  805. };
  806.  
  807. static char *
  808. read_table(desc, size, offset)
  809.     long offset;
  810. {
  811.     char *buf;
  812.     if (lseek(desc, offset, 0) < 0)
  813.       error("Bad symbol table format [seek]");
  814.     buf = xmalloc(size);
  815.     if (buf == NULL) error("Not enough memory");
  816.     if (myread (desc, buf, size) != size)
  817.       error("Bad symbol table format [read]");
  818.     return buf;
  819. }
  820.  
  821. static char * (MapStNames[]) = { /* symbol type names */
  822.     "Nil", "Global", "Static", "Param", "Local", "Label", "Proc", "Block",
  823.     "End", "Member", "Typedef", "File", "RegReloc", "Forward", "StaticProc",
  824.     "Constant", "BlockPatched"
  825. };
  826.  
  827. long stNameSize = sizeof(MapStNames)/sizeof(MapStNames[0]);
  828.  
  829. static char * MapScNames[] = {    /* storage class names */
  830.     "Nil", "Text", "Data", "Bss", "Register", "Abs", "Undefined", "CdbLocal",
  831.     "Bits", "Dbx", "RegImage", "Info", "UserStruct", "SData", "SBss", "RData",
  832.     "Var", "Common", "SCommon", "VarRegister", "Variant", "SUndefined", "Init"
  833. };
  834.  
  835.  
  836. long scNameSize = sizeof(MapScNames)/sizeof(MapScNames[0]);
  837.  
  838. static char *(MapBtNames[]) = {    /* base type names */
  839.     "Nil", "Adr", "Char", "UChar", "Short", "UShort", "Int", "UInt", "Long",
  840.     "ULong", "Float", "Double", "Struct", "Union", "Enum", "Typedef", "Range",
  841.     "Set", "Complex", "DComplex", "Indirect", "FixedDec", "FloatDec", "String",
  842.     "Bit", "Picture"
  843. };
  844.  
  845. long btNameSize = sizeof(MapBtNames)/sizeof(MapBtNames[0]);
  846.  
  847. /* Given pointers to a symbol table in coff style exec file,
  848.    analyze them and create struct symtab's describing the symbols.
  849.    NSYMS is the number of symbols in the symbol table.
  850.    We read them one at a time using read_one_sym ().  */
  851.  
  852. /* stBlockPatched indicates an stBlock symbol which has been patched
  853.  * so that the value points to s struct type */
  854.  
  855. #define stBlockPatched 16
  856.  
  857. static FDR *file_descriptor_table;
  858. static RFDT *rel_file_table;
  859. static SYMR *local_symbol_table;
  860. static char *local_string_table;
  861. static AUXU *aux_symbol_table;
  862. static FDR *cur_file_descriptor;
  863. static int file_descriptor_count;
  864. /* Note that PDR is also used for frame chaining, and is defined above */
  865.  
  866. static int cur_proc_number; /* number of procedure within current file */
  867. static CORE_ADDR cur_proc_addr;
  868. static int cur_file_number;
  869. static int cur_isymBase;
  870. static int cur_issBase;
  871.  
  872. static void
  873. select_file(i)
  874.     int i;
  875. {
  876.     global_symbols_all[cur_file_number] = global_symbols;
  877.     file_symbols_all[cur_file_number] = file_symbols;
  878.     global_symbols = global_symbols_all[i];
  879.     file_symbols = file_symbols_all[i];
  880.     cur_file_number = i;
  881.     cur_file_descriptor = &file_descriptor_table[i];
  882.     cur_isymBase = cur_file_descriptor->isymBase;
  883.     cur_issBase = cur_file_descriptor->issBase;
  884. }
  885.  
  886. /* side-effect: changes cur_file_descriptor */
  887.  
  888. static SYMR *
  889. get_type_context(auxp)
  890.     AUXU** auxp;
  891. {
  892.   SYMR *sym;
  893.   FDR *sym_file_desc;
  894.   int rfi;
  895.   int rfd = (*auxp)->rndx.rfd;
  896.   int sym_index = (*auxp)->rndx.index;
  897.   (*auxp)++;
  898.   if (rfd == ST_RFDESCAPE) { rfd = (*auxp)->isym; (*auxp)++; }
  899.   if (rfd == ST_EXTIFD || sym_index == ST_ANONINDEX) return NULL;
  900.   rfi = rel_file_table[cur_file_descriptor->rfdBase + rfd];
  901.   if (rfi >= file_descriptor_count) return NULL;
  902.   sym_file_desc = &file_descriptor_table[rfi];
  903.   if (sym_index >= sym_file_desc->csym || sym_index == 0) return NULL;
  904.   sym = &local_symbol_table[sym_file_desc->isymBase + sym_index];
  905.   if (sym->index == 0) return NULL;
  906.   select_file(rfi);
  907.   if (dump_stuff)
  908.     printf("rfd(%d,%d)->[%s,%x,st%s,sc%s,inx:%d (%x)]\n",
  909.      rfi, sym_index,
  910.      &local_string_table[cur_issBase+sym->iss],
  911.      sym->value,
  912.            MapStNames[sym->st], MapScNames[sym->sc],
  913.            sym->index, sym);
  914.   return sym;
  915. }
  916.  
  917. static struct type *
  918. get_struct_type(sym, kind)
  919.      SYMR *sym;
  920.      int kind; /* either btStruct or btUnion or btNil (unknown) */
  921. {
  922.   struct type *type;
  923.   if (sym->st == stBlockPatched)
  924.     {
  925.       if (debug) {
  926.       fprintf(stderr, "Hello 1\n");
  927.       }
  928.       type = (struct type*)sym->value;
  929.       /* Patch up possibly-wrong guess about struct vs. union */
  930.       if (kind == btStruct) TYPE_CODE(type) = TYPE_CODE_STRUCT;
  931.       else if (kind == btUnion) TYPE_CODE(type) = TYPE_CODE_UNION;
  932.     }
  933.   else if (sym->st != stBlock)
  934.     {
  935.       fprintf(stderr,"Bad symbol table: struct/union points to non-stBlock\n");
  936.       type = builtin_type_void;
  937.     }
  938.   else
  939.     {
  940.       int nfields = 0;
  941.       SYMR *first_field = sym+1;
  942.       register struct field *cur_field;
  943.       char *sym_name = &local_string_table[cur_issBase+sym->iss];
  944.       int iauxBase = cur_file_descriptor->iauxBase;
  945.       if (debug) {
  946.       fprintf(stderr, "Hello 2\n");
  947.       }
  948.       type = (struct type *) obstack_alloc (symbol_obstack,
  949.                         sizeof (struct type));
  950.       bzero (type, sizeof (struct type));
  951.       TYPE_LENGTH (type) = sym->value;
  952.  
  953.       /* we "remember" the type generated from this block */
  954.       sym->st = stBlockPatched;
  955.       sym->value = (long)type;
  956.  
  957.       /* first count the number of fields */
  958.       for (sym = first_field; sym->st != stEnd; sym++)
  959.     if (sym->st == stMember) nfields++;
  960.     else if (sym->st == stBlock || sym->st == stBlockPatched)
  961.       {
  962.         if (sym->sc == scVariant) ; /* UNIMPLEMENTED */
  963.         if (sym->index != 0)
  964.           sym = &local_symbol_table[cur_isymBase + sym->index - 1];
  965.       }
  966.  
  967.       TYPE_NFIELDS (type) = nfields;
  968.       TYPE_FIELDS (type) = cur_field = (struct field*)
  969.     obstack_alloc (symbol_obstack, nfields * sizeof (struct field));
  970.       for (sym = first_field; sym->st != stEnd; sym++)
  971.     if (sym->st == stMember)
  972.       {
  973.         AUXU *aux = &aux_symbol_table[iauxBase + sym->index];
  974.         char *name = &local_string_table[cur_issBase+sym->iss];
  975.         cur_field->name =
  976.           obstack_copy0 (symbol_obstack, name, strlen (name));
  977.  
  978.         cur_field->type = read_type(sym->index, &cur_field->bitsize);
  979.         cur_field->bitpos = sym->value;
  980.         cur_field++;
  981.       }
  982.     else if (sym->st == stTypedef) ; /* just ignore it */
  983.     else if (sym->st == stBlock || sym->st == stBlockPatched)
  984.       {
  985.         if (sym->sc == scVariant) ; /* UNIMPLEMENTED */
  986.         if (sym->index != 0)
  987.           sym = &local_symbol_table[cur_isymBase + sym->index - 1];
  988.       }
  989.     else
  990.       fprintf(stderr, "[Bad member in struct/union field list]\n");
  991.       /*
  992.        * Heuristic: if 2nd field has offset 0, this is a union,
  993.        * otherwise, a struct definition.
  994.        * If nfields <= 1, we guess struct.
  995.        * If the type is used later, we fix it up.
  996.        * (This is done in the stBlockPatched case at the top of this routine).
  997.        */
  998.       if (kind == btNil && nfields > 1)
  999.     {
  1000.       if (TYPE_FIELDS(type)[1].bitpos > 0) kind = btStruct;
  1001.       else kind = btUnion;
  1002.     }
  1003.       TYPE_CODE (type) = kind == btUnion ? TYPE_CODE_UNION : TYPE_CODE_STRUCT;
  1004.       TYPE_NAME (type) = concat ("",
  1005.                  (kind == btUnion ? "union " : "struct "),
  1006.                  sym_name);
  1007.     }
  1008.     return type;
  1009. }
  1010.  
  1011. static struct type *
  1012. read_struct_type(auxp, kind)
  1013.      AUXU** auxp;
  1014.      int kind; /* either btStruct or btUnion */
  1015. {
  1016.   struct type *type;
  1017.   int save_file_number = cur_file_number;
  1018.   SYMR *sym = get_type_context(auxp);
  1019. if (debug) {
  1020.     fprintf(stderr, "read_struct_type\n");
  1021. }
  1022.   if (sym == NULL) {
  1023.     fprintf(stderr, "Bad symbol for struct/union definition");
  1024.     return builtin_type_void;
  1025.   }
  1026.   if (sym->st == stTypedef) 
  1027. {
  1028. if (debug) {
  1029.     fprintf(stderr, "Debug already set\n");
  1030. }
  1031. fprintf(stderr, "\n>>>\n");
  1032. fflush(stdout);
  1033. debug = 1;
  1034.       type = read_type(sym->index, NULL);
  1035. debug = 0;
  1036. fprintf(stderr, "\n<<<\n");
  1037. fflush(stdout);
  1038. }
  1039.   else
  1040.       type = get_struct_type(sym, kind);
  1041.   select_file (save_file_number);
  1042.   return type;
  1043. }
  1044.  
  1045. static struct type *
  1046. get_enum_type(sym)
  1047.     SYMR *sym;
  1048. {
  1049.   struct type *type;
  1050.   if (sym->st == stBlockPatched)
  1051.     type = (struct type*)sym->value;
  1052.   else if (sym->st != stBlock)
  1053.     {
  1054.       fprintf(stderr,"Bad symbol table: enum points to non-stBlock\n");
  1055.       type = builtin_type_void;
  1056.     }
  1057.   else
  1058.     {
  1059.       int nfields = 0;
  1060.       SYMR *first_field = sym+1;
  1061.       register struct field *cur_field;
  1062.       type = (struct type *) obstack_alloc (symbol_obstack,
  1063.                         sizeof (struct type));
  1064.       bzero (type, sizeof (struct type));
  1065.       TYPE_LENGTH (type) = sym->value;
  1066.  
  1067.       /* we "remember" the type generated from this block */
  1068.       sym->st = stBlockPatched;
  1069.       sym->value = (long)type;
  1070.  
  1071.       TYPE_CODE (type) = TYPE_CODE_ENUM;
  1072.       TYPE_LENGTH (type) = sizeof (int);
  1073.       TYPE_NAME (type) = concat ("", "enum ",
  1074.                  &local_string_table[cur_issBase+sym->iss]);
  1075.  
  1076.       nfields = &local_symbol_table[cur_isymBase+sym->index] - first_field - 1;
  1077.  
  1078.       TYPE_NFIELDS (type) = nfields;
  1079.       TYPE_FIELDS (type) = cur_field = (struct field*)
  1080.     obstack_alloc (symbol_obstack, nfields * sizeof (struct field));
  1081.       for (sym = first_field; sym->st != stEnd; sym++)
  1082.     if (sym->st == stMember)
  1083.       {
  1084.         char *name = &local_string_table[cur_issBase+sym->iss];
  1085.         cur_field->name =
  1086.           obstack_copy0 (symbol_obstack, name, strlen (name));
  1087.  
  1088.         cur_field->bitpos = sym->value;
  1089.         cur_field->bitsize = 0;
  1090.         cur_field++;
  1091.       }
  1092.     else
  1093.       fprintf(stderr, "[Bad member in enum list]\n");
  1094.     }
  1095.   return type;
  1096. }
  1097.  
  1098. static struct type *
  1099. read_enum_type(auxp)
  1100.      AUXU** auxp;
  1101. {
  1102.   struct type *type;
  1103.   int save_file_number = cur_file_number;
  1104.   SYMR *sym = get_type_context(auxp);
  1105.   if (sym == NULL) {
  1106.     printf(stderr, "Bad symbol for enum definition");
  1107.     return builtin_type_int;
  1108.   }
  1109.   if (sym->st == stTypedef)
  1110.       type = read_type(sym->index, NULL);
  1111.   else
  1112.       type = get_enum_type (sym);
  1113.   select_file (save_file_number);
  1114.   return type;
  1115. }
  1116.  
  1117. static struct type *
  1118. read_range_type(auxp)
  1119.      AUXU** auxp;
  1120. {
  1121.   struct type *range_type;
  1122.   int save_file_number = cur_file_number;
  1123.   SYMR *sym = get_type_context(auxp);
  1124.   /* sym (if non-NULL) may point at an stBlock/scInfo, but ignore it */
  1125.    range_type = (struct type *) obstack_alloc (symbol_obstack,
  1126.                           sizeof (struct type));
  1127.   TYPE_CODE (range_type) = TYPE_CODE_RANGE;
  1128.   TYPE_TARGET_TYPE (range_type) = builtin_type_int;
  1129.   TYPE_LENGTH (range_type) = sizeof (int);
  1130.   TYPE_NFIELDS (range_type) = 2;
  1131.   TYPE_FIELDS (range_type) =
  1132.       (struct field *) obstack_alloc (symbol_obstack,
  1133.                       2 * sizeof (struct field));
  1134.   TYPE_FIELD_BITPOS (range_type, 0) = (*auxp)++->dnLow;
  1135.   TYPE_FIELD_BITPOS (range_type, 1) = (*auxp)++->dnHigh;
  1136.  
  1137.   select_file (save_file_number);
  1138.   return range_type;
  1139. }
  1140.  
  1141. static struct type *
  1142. modify_type(type, qualifier, auxp)
  1143.      struct type *type;
  1144.      int qualifier;
  1145.      AUXU** auxp;
  1146. {
  1147.     switch (qualifier) {
  1148.       case tqPtr: return lookup_pointer_type(type);
  1149.       case tqNil: return type;
  1150.       case tqArray:
  1151.     {
  1152.         struct type *range_type = read_range_type(auxp);
  1153.         long lower = TYPE_FIELD_BITPOS (range_type, 0);
  1154.         long upper = TYPE_FIELD_BITPOS (range_type, 1);
  1155.         int elem_size = (*auxp)++->width; /* not used */
  1156.         struct type *atype;
  1157.  
  1158.         atype = (struct type *)
  1159.         obstack_alloc (symbol_obstack, sizeof (struct type));
  1160.         bzero (atype, sizeof (struct type));
  1161.  
  1162.         TYPE_CODE (atype) = TYPE_CODE_ARRAY;
  1163.         TYPE_TARGET_TYPE (atype) = type;
  1164.         TYPE_LENGTH (atype) = (upper - lower + 1) * TYPE_LENGTH (type);
  1165.         TYPE_NFIELDS (atype) = 1;
  1166.         TYPE_FIELDS (atype) =
  1167.         (struct field *) obstack_alloc (symbol_obstack,
  1168.                         sizeof (struct field));
  1169.         TYPE_FIELD_TYPE (atype, 0) = range_type;
  1170.         return atype;
  1171.     }
  1172.       case tqVol: return type;
  1173.       case tqProc: return lookup_function_type(type);
  1174.       default:
  1175.     fprintf(stderr, "[Unimplemented type qualifier: %d]\n", qualifier);
  1176.     return type;
  1177.     }
  1178. }
  1179.  
  1180. static struct type *
  1181. apply_type_modifiers(type, tip, auxp)
  1182.      struct type *type;
  1183.      TIR *tip;
  1184.      AUXU** auxp;
  1185. {
  1186.     for (;;) {   
  1187.     if (tip->tq0 == tqNil) return type;
  1188.     type = modify_type(type, tip->tq0, auxp);
  1189.     if (tip->tq1 == tqNil) return type;
  1190.     type = modify_type(type, tip->tq1, auxp);
  1191.     if (tip->tq2 == tqNil) return type;
  1192.     type = modify_type(type, tip->tq2, auxp);
  1193.     if (tip->tq3 == tqNil) return type;
  1194.     type = modify_type(type, tip->tq3, auxp);
  1195.     if (tip->tq4 == tqNil) return type;
  1196.     type = modify_type(type, tip->tq4, auxp);
  1197.     if (tip->tq5 == tqNil) return type;
  1198.     type = modify_type(type, tip->tq5, auxp);
  1199.     if (!tip->continued) return type;
  1200.     tip++;
  1201.     }
  1202. }
  1203.  
  1204. static struct type *
  1205. read_type(index, widthp)
  1206.      int index;
  1207.      int *widthp; /* if non-NULL: set to (if bit field: width, else: 0) */
  1208. {
  1209.     TIR *tip;
  1210.     AUXU *aux;
  1211.     struct type *base_type;
  1212.     int width;
  1213.     if (index == 0xfffff) return builtin_type_int; /* no type info */
  1214. /*    if (index < 0 || index > ) ...; */
  1215.  
  1216.     aux = &aux_symbol_table[cur_file_descriptor->iauxBase + index];
  1217.     tip = &aux->ti;
  1218.     for ( ; aux->ti.continued; aux++) ;
  1219.     aux++; /* skip last TIR field */
  1220.  
  1221.     /* sym.h claims that width comes after RNDX, but seems to be wrong */
  1222.     width = tip->fBitfield ? (aux++)->width : 0;
  1223.     if (widthp) *widthp = width;
  1224. if (debug) {
  1225. fprintf(stderr, "\n(%d)\n", tip->bt);
  1226. fflush(stdout);
  1227. }
  1228.     switch (tip->bt) {
  1229.       case btNil: base_type = builtin_type_void; break;
  1230.       case btAdr: base_type = lookup_pointer_type(builtin_type_void); break;
  1231.       case btChar: base_type = builtin_type_char; break;
  1232.       case btUChar: base_type = builtin_type_unsigned_char; break;
  1233.       case btShort: base_type = builtin_type_short; break;
  1234.       case btUShort: base_type = builtin_type_unsigned_short; break;
  1235.       case btInt: base_type = builtin_type_int; break;
  1236.       case btUInt: base_type = builtin_type_unsigned_int; break;
  1237.       case btLong: base_type = builtin_type_long; break;
  1238.       case btULong: base_type = builtin_type_unsigned_long; break;
  1239.       case btFloat: base_type = builtin_type_float; break;
  1240.       case btDouble: base_type = builtin_type_double; break;
  1241.       case btStruct: case btUnion:
  1242.     base_type = read_struct_type(&aux, tip->bt); break;
  1243.       case btEnum:
  1244.     base_type = read_enum_type(&aux); break;
  1245.       case btRange:
  1246.     base_type = read_range_type(&aux); break;
  1247.       case btTypedef:
  1248.       case btSet: case btComplex: case btDComplex:
  1249.       case btIndirect:
  1250.       case btFixedDec: case btFloatDec: case btString:
  1251.       case btBit: case btPicture:
  1252.       default:
  1253.     fprintf(stderr, "[Unimplemented kind of type: %d]\n", tip->bt);
  1254.     base_type = builtin_type_void;
  1255.     }
  1256.     return apply_type_modifiers(base_type, tip, &aux);   
  1257. }
  1258.  
  1259. static struct symbol *
  1260. alloc_symbol(name, value)
  1261.   char *name;
  1262.   int value;
  1263. {
  1264.   register struct symbol *sym;
  1265.   sym = (struct symbol *)obstack_alloc (symbol_obstack, sizeof(struct symbol));
  1266. #ifdef NAMES_HAVE_UNDERSCORE
  1267.   if (name[0] == '_') name++;
  1268. #endif
  1269.  
  1270.   bzero (sym, sizeof (struct symbol));
  1271.   SYMBOL_NAME (sym) = obstack_copy0 (symbol_obstack, name, strlen (name));
  1272.  
  1273.   /* default assumptions */
  1274.   SYMBOL_VALUE (sym) = value;
  1275.   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1276.   SYMBOL_TYPE (sym) = builtin_type_void;
  1277.  
  1278.   return sym;
  1279. }
  1280.  
  1281. static
  1282. read_symbol(csym, name)
  1283.   SYMR *csym;
  1284.   char *name;
  1285. {
  1286.   register struct symbol *sym = alloc_symbol(name, csym->value);
  1287.  
  1288.   switch (csym->st)
  1289.     {
  1290.     case stNil: break;
  1291.     case stFile:
  1292.       last_source_file = obstack_copy0 (symbol_obstack, name, strlen (name));
  1293.       break;
  1294.     case stGlobal:
  1295.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1296.       SYMBOL_CLASS (sym) = LOC_STATIC;
  1297.       add_symbol_to_list (sym, &global_symbols);
  1298.       break;
  1299.     case stStatic:
  1300.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1301.       SYMBOL_CLASS (sym) = LOC_STATIC;
  1302.       if (within_function) {
  1303.       /* Static symbol of local scope */
  1304.       add_symbol_to_list (sym, &local_symbols);
  1305.       }
  1306.       else {
  1307.       /* Static symbol at top level of file */
  1308.       add_symbol_to_list (sym, &file_symbols);
  1309.       }
  1310.       break;
  1311.  
  1312.     case stLocal:
  1313.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1314.       switch (csym->sc) {
  1315.     case scRegister: SYMBOL_CLASS (sym) = LOC_REGISTER; break;
  1316.     case scAbs:    SYMBOL_CLASS (sym) = LOC_LOCAL; break;
  1317.  
  1318.       }
  1319.       add_symbol_to_list (sym, &local_symbols);
  1320.       break;
  1321.  
  1322.     case stParam:
  1323.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1324.       switch (csym->sc) {
  1325.     case scAbs:      SYMBOL_CLASS (sym) = LOC_ARG; break;
  1326.     case scRegister:  SYMBOL_CLASS (sym) = LOC_REGPARM; break;
  1327.     case scVar:      SYMBOL_CLASS (sym) = LOC_REF_ARG; break;
  1328.     case scVarRegister: SYMBOL_CLASS (sym) = LOC_REGPARM; break; /*WRONG!*/
  1329.       }
  1330.       add_symbol_to_list (sym, &local_symbols);
  1331.       break;
  1332.  
  1333.     case stTypedef:
  1334.       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1335.       SYMBOL_TYPE (sym) = read_type(csym->index, NULL);
  1336.       if (within_function) {
  1337.       /* Static symbol of local scope */
  1338.       add_symbol_to_list (sym, &local_symbols);
  1339.       }
  1340.       else {
  1341.       /* Static symbol at top level of file */
  1342.       add_symbol_to_list (sym, &file_symbols);
  1343.       }
  1344.       break;
  1345.     
  1346.     case stEnd:
  1347.     default: break;
  1348.     }
  1349.   return 1;
  1350. }
  1351.  
  1352. static int
  1353. read_tagged_block (index)
  1354. {
  1355.   SYMR *csym = &local_symbol_table[cur_isymBase+index];
  1356.   char *name = &local_string_table[cur_issBase+csym->iss];
  1357.   struct pending **symlist = within_function ? &local_symbols: &file_symbols;
  1358.   register struct symbol *sym;
  1359.  
  1360.   /* A kludge to decide if this is an enum definition. */
  1361.   if ((csym[1].st == stMember
  1362.        && read_type(csym[1].index, 0) == builtin_type_void)
  1363.    || (csym->st == stBlockPatched
  1364.        && TYPE_CODE((struct type*)(csym->value)) == TYPE_CODE_ENUM))
  1365.     {
  1366.       struct type *type = get_enum_type (csym);
  1367.       if (type && TYPE_CODE(type) == TYPE_CODE_ENUM)
  1368.         {
  1369.           int nfields = TYPE_NFIELDS (type);
  1370.           register int i;
  1371.           register struct field *cur_field = TYPE_FIELDS(type);
  1372.           if (name && name[0] && name[0] != '.')
  1373.         {
  1374.           sym = alloc_symbol(name, 0);
  1375.           SYMBOL_TYPE (sym) = type;
  1376.           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1377.           SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1378.           add_symbol_to_list (sym, symlist);
  1379.         }
  1380.           for (i = nfields; --i >= 0; cur_field++)
  1381.         {
  1382.           sym = alloc_symbol(cur_field->name, cur_field->bitpos);
  1383.           SYMBOL_CLASS (sym) = LOC_CONST;
  1384.           SYMBOL_TYPE (sym) = type;
  1385.           add_symbol_to_list (sym, symlist);
  1386.         }
  1387.           return nfields + 2;
  1388.       }
  1389.       }
  1390.   else
  1391.     {
  1392.       if (name && name[0] && name[0] != '.')
  1393.     {
  1394.       struct type *type = get_struct_type (csym, btNil);
  1395.       sym = alloc_symbol(name, 0);
  1396.       SYMBOL_TYPE (sym) = type;
  1397.       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1398.       SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1399.       add_symbol_to_list (sym, symlist);
  1400.     }
  1401.       return read_symbols (index + 1) + 2; 
  1402.     }
  1403. }
  1404.  
  1405. /* Read symbols until an stEnd symbol is encountered. Return count. */
  1406.  
  1407. static int
  1408. read_symbols(index)
  1409.      int index; /* relative to current file */
  1410. {
  1411.   register struct context_stack *new;
  1412.   static int blocks_seen = 0;
  1413.   int index0 = index;
  1414.   SYMR *start_symbol;
  1415.   for (;;)
  1416.       {
  1417.     SYMR *csym = &local_symbol_table[cur_isymBase+index];
  1418.     char *name = &local_string_table[cur_issBase+csym->iss];
  1419.  
  1420.     if (dump_stuff)
  1421.         printf("[%s,%x,st%s,sc%s,inx:%d (%x)]\n", name, csym->value,
  1422.            MapStNames[csym->st], MapScNames[csym->sc],
  1423.            csym->index, csym);
  1424.  
  1425.     switch (csym->st)
  1426.         {
  1427.         case stFile:
  1428.           last_source_file =
  1429.           obstack_copy0 (symbol_obstack, name, strlen (name));
  1430.           index++;
  1431.           index += read_symbols(index) + 1;
  1432.           return index - index0;
  1433.         case stBlock:
  1434.         case stBlockPatched:
  1435.           if (csym->sc == scText)
  1436.         { /* lexical block */
  1437.           if (!local_symbol_table[cur_isymBase+csym->index-1].value)
  1438.             {
  1439.               new = 0;
  1440.             }
  1441.           else
  1442.             {
  1443.               new = (struct context_stack *)
  1444.             xmalloc (sizeof (struct context_stack));
  1445.               new->next = context_stack;
  1446.               context_stack = new;
  1447.               new->locals = local_symbols;
  1448.               new->old_blocks = pending_blocks;
  1449.               new->start_addr = cur_proc_addr + csym->value;
  1450.               new->name = 0;
  1451.               local_symbols = 0;
  1452.             }
  1453.           blocks_seen++;
  1454.           start_symbol=csym;
  1455.           index++;
  1456.           index += read_symbols (index);
  1457.           csym = &local_symbol_table[cur_isymBase+index];  /* stEnd */
  1458.           index++;
  1459.  
  1460. if (csym->st != stEnd ||
  1461.    start_symbol != &local_symbol_table[cur_isymBase+csym->index])
  1462. abort();
  1463.           if (new)
  1464.             {
  1465.               if (local_symbols && context_stack->next)
  1466.             {
  1467.               /* Make a block for the local symbols within.  */
  1468.               finish_block (0, &local_symbols, new->old_blocks,
  1469.                     new->start_addr,
  1470.                     cur_proc_addr + csym->value);
  1471.             }
  1472.               local_symbols = new->locals;
  1473.               context_stack = new->next;
  1474.               free (new);
  1475.             }
  1476.           }
  1477.           else if (csym->sc == scInfo)
  1478.           index += read_tagged_block (index);
  1479.           break;
  1480.         case stProc:
  1481.         case stStaticProc:
  1482.           {
  1483.         PDR *proc = &proc_desc_table
  1484.             [cur_file_descriptor->ipdFirst+cur_proc_number++];
  1485.         CORE_ADDR saved_cur_proc_addr = cur_proc_addr;
  1486.         struct symbol *sym = alloc_symbol(name, csym->value);
  1487.         struct context_stack *saved_context_stack = context_stack;
  1488.         int save_blocks_seen = blocks_seen;
  1489.         SYMBOL_CLASS (sym) = LOC_BLOCK;
  1490.         /* Add one to csym->index, to the skip the isymMac value
  1491.          * (which is there only for st*Proc). */
  1492.         SYMBOL_TYPE (sym) = 
  1493.             lookup_function_type (read_type(csym->index+1, NULL));
  1494.         if (csym->st == stStaticProc)
  1495.             add_symbol_to_list (sym, &file_symbols);
  1496.         else
  1497.             add_symbol_to_list (sym, &global_symbols);
  1498.         cur_proc_addr = csym->value;
  1499.         within_function++;
  1500.         new = (struct context_stack *)
  1501.             xmalloc (sizeof (struct context_stack));
  1502.         new->next = 0;
  1503.         context_stack = new;
  1504.         new->locals = 0;
  1505.         new->old_blocks = pending_blocks;
  1506.         new->start_addr = csym->value;
  1507.         new->name = sym;
  1508.         if (dump_stuff)
  1509.             printf("[PROC:%s,@%x,framereg:%d,froff:%d,rmsk:%d,fmsk:%d,floff:%d,regoff:%d,iline:%d,lnLo:%d,lnHi:%d (%x)]\n",
  1510.                name, proc->adr,
  1511.            proc->framereg, proc->frameoffset, proc->regmask,
  1512.            proc->fregmask, proc->fregoffset,
  1513.                proc->regoffset,
  1514.                proc->iline,proc->lnLow,proc->lnHigh, proc);
  1515.  
  1516.         start_symbol = csym;
  1517.         index++;
  1518.         index += read_symbols(index);
  1519.  
  1520.         csym = &local_symbol_table[cur_isymBase+index];/*stEnd symbol*/
  1521.  
  1522. if (csym->st != stEnd ||
  1523.    start_symbol != &local_symbol_table[cur_isymBase+csym->index])
  1524. abort();
  1525.  
  1526.         index++;
  1527.         PROC_SYMBOL(proc) =
  1528.             blocks_seen == save_blocks_seen ? NULL : sym;
  1529.         PROC_LOW_ADDR(proc) = start_symbol->value;
  1530.         PROC_HIGH_ADDR(proc) = last_end_addr =
  1531.             start_symbol->value + csym->value;
  1532.  
  1533.         finish_block (new->name, &local_symbols, new->old_blocks,
  1534.                   start_symbol->value, last_end_addr);
  1535.         context_stack = saved_context_stack;
  1536.         cur_proc_addr = saved_cur_proc_addr;
  1537.         within_function--;
  1538.         free (new);
  1539.         break;
  1540.           }
  1541.  
  1542.         case stEnd:
  1543. #if 1
  1544.             return index - index0;
  1545. #else
  1546.           start_symbol = &local_symbol_table[cur_isymBase+csym->index];
  1547.           switch (start_symbol->st)
  1548.           {
  1549.           case stProc:
  1550.           case stStaticProc:
  1551.           case stFile:
  1552.           case stBlock:
  1553.           case stBlockPatched:
  1554.             return index - index0;
  1555.           default:
  1556.             index++;
  1557.           }
  1558.           break;
  1559. #endif
  1560.         default:
  1561.           read_symbol(csym, name);
  1562.           index++;
  1563.         }
  1564.       }
  1565. }
  1566.  
  1567. static int
  1568. read_coff_symtab (desc, nsyms, symtab_offset)
  1569.      int desc;
  1570.      int nsyms;
  1571.      int symtab_offset;
  1572. {
  1573.   HDRR hdrr;
  1574.   struct coff_symbol coff_symbol;
  1575.   register struct coff_symbol *cs = &coff_symbol;
  1576.   struct coff_symbol fcn_cs_saved;
  1577.  
  1578.   int num_object_files = 0;
  1579.   int next_file_symnum = -1;
  1580.   char *filestring;
  1581.   int fcn_first_line;
  1582.   int fcn_last_line;
  1583.   int fcn_start_addr;
  1584.   long fcn_line_ptr;
  1585.   struct cleanup *file_chain, *ext_chain, *old_chain;
  1586.   int isym, ifile;
  1587.  
  1588.   char *line_number_table;
  1589.   EXTR *external_symbol_table;
  1590.   char *external_string_table;
  1591.   struct linetable **line_vector_all;
  1592.  
  1593.   if (myread (desc, (char *)&hdrr, sizeof hdrr) != sizeof hdrr)
  1594.     return -1;
  1595.  
  1596.   file_descriptor_count = hdrr.ifdMax;
  1597.   line_vector_all = (struct linetable**)
  1598.       alloca (file_descriptor_count * sizeof(struct linetable *));
  1599.   global_symbols_all = (struct pending**)
  1600.       alloca (file_descriptor_count * sizeof(struct pending *));
  1601.   file_symbols_all = (struct pending**)
  1602.       alloca (file_descriptor_count * sizeof(struct pending *));
  1603.   proc_desc_length = hdrr.ipdMax;
  1604.   proc_desc_table = (PDR*)
  1605.     read_table(desc, hdrr.ipdMax * sizeof(PDR), hdrr.cbPdOffset);
  1606.   old_chain = make_cleanup (free_all_symtabs, 0);
  1607.   make_cleanup (free_proc_descs, 0);
  1608.   file_descriptor_table = (FDR*)
  1609.     read_table(desc, hdrr.ifdMax * sizeof(FDR), hdrr.cbFdOffset);
  1610.   file_chain = make_cleanup (free, file_descriptor_table);
  1611.  
  1612.   line_number_table = read_table(desc, hdrr.cbLine, hdrr.cbLineOffset);
  1613.   ext_chain = make_cleanup (free, line_number_table);
  1614.  
  1615.   for (ifile = 0; ifile < hdrr.ifdMax; ifile++)
  1616.     {
  1617.       int iproc, ipdMax;
  1618.       char *cur_line_entry;
  1619.       int cur_addr_offset = 0;
  1620.       int cur_addr;
  1621.  
  1622.       /* Vector of line number information.  */
  1623.       struct linetable *line_vector;
  1624.  
  1625.       /* Index of next entry to go in line_vector_index.  */
  1626.       int line_vector_index;
  1627.  
  1628.       /* Number of elements allocated for line_vector currently.  */
  1629.       int line_vector_length;
  1630.  
  1631.       global_symbols_all[ifile] = 0;
  1632.       file_symbols_all[ifile] = 0;
  1633.       select_file (ifile);
  1634.  
  1635.       /* read line numbers */
  1636.       cur_addr = cur_file_descriptor->adr;
  1637.       iproc = cur_file_descriptor->ipdFirst;
  1638.  
  1639.       line_vector_index = 0;
  1640.       line_vector_length = 1000;
  1641.       line_vector = (struct linetable *)
  1642.       xmalloc (sizeof (struct linetable)
  1643.            + line_vector_length * sizeof (struct linetable_entry));
  1644.  
  1645.       /* we read the line numbers first, since read_symbol over-writes
  1646.      some of the fields in a proc descriptor */
  1647.       ipdMax = iproc + cur_file_descriptor->cpd;
  1648.       cur_line_entry = line_number_table + cur_file_descriptor->cbLineOffset;
  1649.       for ( ; iproc < ipdMax; iproc++) {
  1650.     PDR *proc = &proc_desc_table[iproc];
  1651.     int nlines = (iproc+1 < ipdMax ? (proc+1)->iline : cur_file_descriptor->cline)
  1652.         - proc->iline;
  1653.     int cur_source_line = proc->lnLow;
  1654.     if (dump_stuff)
  1655.         printf(
  1656.         "[proc:%x,framereg:%d,ofset:%d,rmask:%d,flmask:%d,floff:%d,iline:%d,lnLo:%d,lnHi:%d,csl:%d]\n",
  1657.            proc->adr,
  1658.            proc->framereg, proc->frameoffset, proc->regmask,
  1659.            proc->fregmask, proc->fregoffset,
  1660.            proc->iline,proc->lnLow,proc->lnHigh, cur_source_line);
  1661.     if (proc->iline == -1) continue;
  1662.     for (isym = proc->iline; nlines > 0; isym++) {
  1663.       struct linetable_entry *e;
  1664.       int code = *cur_line_entry++;
  1665.       int delta = code >> 4;
  1666.       int count = (code & 15) + 1;
  1667.       if (dump_stuff & 1) printf("%2x", 0xFF & code);
  1668.       if (delta == -8)
  1669.         {
  1670.           if (dump_stuff & 1)
  1671.             printf(" %2x%2x", 0xff & cur_line_entry[0],
  1672.              0xff & cur_line_entry[1]);
  1673.           delta = *cur_line_entry++;
  1674.           delta = (delta << 8) | (unsigned)*cur_line_entry++;
  1675.           isym += 2;
  1676.         }
  1677.       cur_source_line += delta;
  1678.       if (dump_stuff & 1)
  1679.         printf("\t%2d.%2d li:%d %X", delta, count,
  1680.          cur_source_line,
  1681.          cur_file_descriptor->adr + 4 * cur_addr_offset);
  1682.  
  1683.       e = &line_vector->item[line_vector_index];
  1684.       if (line_vector_index == 0 || e[-1].line != cur_source_line)
  1685.         {
  1686.  
  1687.           /* Make sure line vector is big enough.  */
  1688.           if (line_vector_index + 2 >= line_vector_length)
  1689.         {
  1690.           line_vector_length *= 2;
  1691.           line_vector = (struct linetable *)
  1692.             xrealloc (line_vector, sizeof (struct linetable)
  1693.                   + (line_vector_length
  1694.                  * sizeof (struct linetable_entry)));
  1695.           e = &line_vector->item[line_vector_index];
  1696.         }
  1697.  
  1698.           e->line = cur_source_line;
  1699.           e->pc = cur_file_descriptor->adr + 4 * cur_addr_offset;
  1700.           line_vector_index++;
  1701.         }
  1702.  
  1703.       if (delta != 0)
  1704.         {
  1705.           int save_addr = cur_addr;
  1706.           if (cur_addr_offset != 0)
  1707.         {
  1708.           int toffset = (cur_addr_offset - 1) * 4;
  1709.           if (dump_stuff & 1)
  1710.           printf(" [li: %d %x-%x]",
  1711.              cur_source_line, cur_addr, cur_addr+toffset);
  1712.         }
  1713.           cur_addr = save_addr + cur_addr_offset * 4;
  1714.         }
  1715.       cur_addr_offset += count;
  1716.       nlines -= count;
  1717.       if (dump_stuff & 1) putchar('\n');
  1718.     /*  if (delta != 0) { } */
  1719.         }
  1720.       }
  1721.  
  1722.       line_vector->nitems = line_vector_index;
  1723.       line_vector_all[ifile] = (struct linetable *)
  1724.       xrealloc (line_vector, (sizeof (struct linetable)
  1725.                + line_vector_index * sizeof (struct linetable_entry)));
  1726.     }
  1727.   do_cleanups (ext_chain);
  1728.  
  1729.   aux_symbol_table = (AUXU*)
  1730.     read_table(desc, hdrr.iauxMax * sizeof(AUXU), hdrr.cbAuxOffset);
  1731.   make_cleanup (free, aux_symbol_table);
  1732.   rel_file_table = (RFDT*)
  1733.     read_table(desc, hdrr.crfd * sizeof(RFDT), hdrr.cbRfdOffset);
  1734.   make_cleanup (free, rel_file_table);
  1735.   local_symbol_table = (SYMR*)
  1736.     read_table(desc, hdrr.isymMax * sizeof(SYMR), hdrr.cbSymOffset);
  1737.   make_cleanup (free, local_symbol_table);
  1738.   local_string_table = read_table(desc, hdrr.issMax, hdrr.cbSsOffset);
  1739.   make_cleanup (free, local_string_table);
  1740.  
  1741.   external_symbol_table = (EXTR*)
  1742.     read_table(desc, hdrr.iextMax * sizeof(EXTR), hdrr.cbExtOffset);
  1743.   ext_chain = make_cleanup (free, external_symbol_table);
  1744.   external_string_table = read_table(desc, hdrr.issExtMax, hdrr.cbSsExtOffset);
  1745.   make_cleanup (free, external_string_table);
  1746.  
  1747.   /* do the external symbols first */
  1748.   /* The reason is that each file's end_symtab is done with the locals */
  1749.  
  1750.   for (isym = 0; isym < hdrr.iextMax; isym++)
  1751.     {
  1752.       EXTR *ext = &external_symbol_table[isym];
  1753.       SYMR *csym = &ext->asym;
  1754.       char *name = external_string_table+csym->iss;
  1755.       if ((unsigned)ext->ifd >= hdrr.ifdMax) return -1;
  1756.       select_file (ext->ifd);
  1757.       record_misc_function (name, csym->value);
  1758.       if (csym->st != stProc && csym->st != stStaticProc)
  1759.       read_symbol(csym, name);
  1760. #if 1
  1761. if (dump_stuff)
  1762.       printf("EXT[%s,%x,st%s,sc%s,inx:%d,ifd:%d]\n",
  1763.          name, csym->value, MapStNames[csym->st],
  1764.          MapScNames[csym->sc], csym->index, ext->ifd);
  1765. #endif
  1766.     }
  1767.  
  1768.   do_cleanups (ext_chain);
  1769.  
  1770.   for (ifile = 0; ifile < hdrr.ifdMax; ifile++)
  1771.     {
  1772.       select_file (ifile);
  1773.  
  1774.       cur_proc_number = 0;
  1775.       last_end_addr = cur_file_descriptor->adr;
  1776.  
  1777.       start_symtab ();
  1778.  
  1779. if (dump_stuff)
  1780. printf("<FILE.adr:%x,ilB:%d,cb:%d,cbLO:%d,cbL:%d,iSB:%d,cS:%d,nProcs:%d,first:%d>\n",
  1781.        cur_file_descriptor->adr,
  1782.        cur_file_descriptor->ilineBase, cur_file_descriptor->cline,
  1783.        cur_file_descriptor->cbLineOffset, cur_file_descriptor->cbLine,
  1784.        cur_isymBase, cur_file_descriptor->csym,
  1785.        cur_file_descriptor->cpd,
  1786.        cur_file_descriptor->ipdFirst);
  1787.  
  1788.       for (isym = 0; isym < cur_file_descriptor->csym; )
  1789.       {
  1790.         int count = read_symbols(isym);
  1791.         if (count == 0)
  1792.         { /* handle un-handled stEnd */
  1793.           SYMR *sym = &local_symbol_table[cur_isymBase+isym];
  1794.           char *name = &local_string_table[cur_issBase+sym->iss];
  1795. printf("stEnd confusion!");
  1796.           read_symbol(sym, name);
  1797.           count = 1;
  1798.         }
  1799.         isym += count;
  1800.       }
  1801.       end_symtab (cur_file_descriptor->adr, last_end_addr,
  1802.           line_vector_all[ifile]);
  1803.     }
  1804.  
  1805.   do_cleanups (file_chain);
  1806.  
  1807.   last_source_file = 0;
  1808.   bzero (opaque_type_chain, sizeof opaque_type_chain);
  1809.  
  1810.   type_vector_length = 160;
  1811.   type_vector = (struct typevector *)
  1812.         xmalloc (sizeof (struct typevector)
  1813.                 + type_vector_length * sizeof (struct type *));
  1814.   bzero (type_vector->type, type_vector_length * sizeof (struct type *));
  1815.  
  1816.   if (last_source_file)
  1817.     end_symtab ();
  1818.   discard_cleanups (old_chain);
  1819.   return 0;
  1820. }
  1821.  
  1822. /* Routines for reading headers and symbols from executable.  */
  1823.  
  1824. /* Read COFF file header, check magic number,
  1825.    and return number of symbols. */
  1826. read_file_hdr (chan, file_hdr)
  1827.     int chan;
  1828.     FILHDR *file_hdr;
  1829. {
  1830.   lseek (chan, 0L, 0);
  1831.   if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
  1832.     return -1;
  1833.  
  1834.   switch (file_hdr->f_magic)
  1835.     {
  1836. #ifdef MIPSEBMAGIC
  1837.     case MIPSEBMAGIC:
  1838. #endif
  1839. #ifdef MIPSELMAGIC
  1840.     case MIPSELMAGIC:
  1841. #endif
  1842. #ifdef MC68MAGIC
  1843.     case MC68MAGIC:
  1844. #endif
  1845. #ifdef NS32GMAGIC
  1846.       case NS32GMAGIC:
  1847.       case NS32SMAGIC:
  1848. #endif
  1849. #ifdef I386MAGIC
  1850.     case I386MAGIC:
  1851. #endif
  1852. #ifdef CLIPPERMAGIC
  1853.     case CLIPPERMAGIC:
  1854. #endif      
  1855.     return file_hdr->f_nsyms;
  1856.  
  1857.       default:
  1858. #ifdef BADMAG
  1859.     if (BADMAG(file_hdr))
  1860.       return -1;
  1861.     else
  1862.       return file_hdr->f_nsyms;
  1863. #else
  1864.     return -1;
  1865. #endif
  1866.     }
  1867. }
  1868.  
  1869. read_aout_hdr (chan, aout_hdr, size)
  1870.     int chan;
  1871.     AOUTHDR *aout_hdr;
  1872.     int size;
  1873. {
  1874.   lseek (chan, (long)FILHSZ, 0);
  1875.   if (size != sizeof (AOUTHDR))
  1876.     return -1;
  1877.   if (myread (chan, (char *)aout_hdr, size) != size)
  1878.     return -1;
  1879.   return 0;
  1880. }
  1881.  
  1882. read_section_hdr (chan, section_name, section_hdr, nsects)
  1883.     register int chan;
  1884.     register char *section_name;
  1885.     SCNHDR *section_hdr;
  1886.     register int nsects;
  1887. {
  1888.   register int i;
  1889.  
  1890.   if (lseek (chan, FILHSZ + sizeof (AOUTHDR), 0) < 0)
  1891.     return -1;
  1892.  
  1893.   for (i = 0; i < nsects; i++)
  1894.     {
  1895.       if (myread (chan, (char *)section_hdr, SCNHSZ) < 0)
  1896.     return -1;
  1897.       if (strncmp (section_hdr->s_name, section_name, 8) == 0)
  1898.     return 0;
  1899.     }
  1900.     return -1;
  1901. }
  1902.  
  1903.  
  1904. /* Support for string table handling */
  1905.  
  1906. static char *stringtab = NULL;
  1907.  
  1908.  
  1909. #if 0
  1910. static char *
  1911. getsymname (symbol_entry)
  1912.     SYMENT *symbol_entry;
  1913. {
  1914.   static char buffer[SYMNMLEN+1];
  1915.   char *result;
  1916.  
  1917.   if (symbol_entry->n_zeroes == 0)
  1918.     {
  1919.       result = stringtab + symbol_entry->n_offset;
  1920.     }
  1921.   else
  1922.     {
  1923.       strncpy (buffer, symbol_entry->n_name, SYMNMLEN);
  1924.       buffer[SYMNMLEN] = '\0';
  1925.       result = buffer;
  1926.     }
  1927.   return result;
  1928. }
  1929. #endif
  1930.  
  1931. /* Support for line number handling */
  1932. static char *linetab = NULL;
  1933. static long linetab_offset;
  1934. static int linetab_count;
  1935.  
  1936. static int
  1937. hashname (name)
  1938.      char *name;
  1939. {
  1940.   register char *p = name;
  1941.   register int total = p[0];
  1942.   register int c;
  1943.  
  1944.   c = p[1];
  1945.   total += c << 2;
  1946.   if (c)
  1947.     {
  1948.       c = p[2];
  1949.       total += c << 4;
  1950.       if (c)
  1951.     total += p[3] << 6;
  1952.     }
  1953.   
  1954.   return total % HASHSIZE;
  1955. }
  1956.  
  1957. static void
  1958. patch_type (type, real_type)
  1959.     struct type *type;
  1960.     struct type *real_type;
  1961. {
  1962.   register struct type *target = TYPE_TARGET_TYPE (type);
  1963.   register struct type *real_target = TYPE_TARGET_TYPE (real_type);
  1964.   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
  1965.  
  1966.   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
  1967.   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
  1968.   TYPE_FIELDS (target) = (struct field *)
  1969.                 obstack_alloc (symbol_obstack, field_size);
  1970.  
  1971.   bcopy (TYPE_FIELDS (real_target), TYPE_FIELDS (target), field_size);
  1972.  
  1973.   if (TYPE_NAME (real_target))
  1974.     {
  1975.       if (TYPE_NAME (target))
  1976.     free (TYPE_NAME (target));
  1977.       TYPE_NAME (target) = concat (TYPE_NAME (real_target), "", "");
  1978.     }
  1979. }
  1980.  
  1981. /* Patch up all appropriate typdef symbols in the opaque_type_chains
  1982.    so that they can be used to print out opaque data structures properly */
  1983.  
  1984. static void
  1985. patch_opaque_types ()
  1986. {
  1987.   struct symtab *s;
  1988.  
  1989.   /* Look at each symbol in the per-file block of each symtab.  */
  1990.   for (s = symtab_list; s; s = s->next)
  1991.     {
  1992.       register struct block *b;
  1993.       register int i;
  1994.  
  1995.       /* Go through the per-file symbols only */
  1996.       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1);
  1997.       for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
  1998.     {
  1999.       register struct symbol *real_sym;
  2000.  
  2001.       /* Find completed typedefs to use to fix opaque ones.
  2002.          Remove syms from the chain when their types are stored,
  2003.          but search the whole chain, as there may be several syms
  2004.          from different files with the same name.  */
  2005.       real_sym = BLOCK_SYM (b, i);
  2006.       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
  2007.           SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
  2008.           TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
  2009.           TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
  2010.         {
  2011.           register char *name = SYMBOL_NAME (real_sym);
  2012.           register int hash = hashname (name);
  2013.           register struct symbol *sym, *prev;
  2014.  
  2015.           prev = 0;
  2016.           for (sym = opaque_type_chain[hash]; sym;)
  2017.         {
  2018.           if (name[0] == SYMBOL_NAME (sym)[0] &&
  2019.               !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
  2020.             {
  2021.               if (prev)
  2022.             SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
  2023.               else
  2024.             opaque_type_chain[hash]
  2025.               = (struct symbol *) SYMBOL_VALUE (sym);
  2026.  
  2027.               patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
  2028.  
  2029.               if (prev)
  2030.             sym = (struct symbol *) SYMBOL_VALUE (prev);
  2031.               else
  2032.             sym = opaque_type_chain[hash];
  2033.             }
  2034.           else
  2035.             {
  2036.               prev = sym;
  2037.               sym = (struct symbol *) SYMBOL_VALUE (sym);
  2038.             }
  2039.         }
  2040.         }
  2041.     }
  2042.     }
  2043. }
  2044.  
  2045. /* This function is really horrible, but to avoid it, there would need
  2046.    to be more filling in of forward references.  THIS SHOULD BE MOVED
  2047.    OUT OF COFFREAD.C AND DBXREAD.C TO SOME PLACE WHERE IT CAN BE SHARED. */
  2048. int
  2049. fill_in_vptr_fieldno (type)
  2050.      struct type *type;
  2051. {
  2052.   if (TYPE_VPTR_FIELDNO (type) < 0)
  2053.     TYPE_VPTR_FIELDNO (type) =
  2054.       fill_in_vptr_fieldno (TYPE_BASECLASS (type, 1));
  2055.   return TYPE_VPTR_FIELDNO (type);
  2056. }
  2057.  
  2058. /* partial symbol tables are not implemented in coff, therefore
  2059.    block_for_pc() (and others) will never decide to call this. */
  2060.  
  2061. extern struct symtab *
  2062. psymtab_to_symtab ()
  2063. {
  2064.   fatal ("error: Someone called psymtab_to_symtab\n");
  2065. }
  2066.  
  2067. /* These will stay zero all the time */
  2068. struct psymbol_allocation_list global_psymbols, static_psymbols;
  2069.